package com.equipmentmanage.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.equipmentmanage.model.Department;
import com.equipmentmanage.model.PageBean;
import com.equipmentmanage.service.DepartmentService;
import com.equipmentmanage.service.UserService;
import com.equipmentmanage.util.PageUtil;
import com.equipmentmanage.util.ResponseUtil;
import com.equipmentmanage.util.StringUtil;
/*    */ 
/*    */ @Controller
/*    */ @RequestMapping({"/department"})
/*    */ public class DepartmentController
/*    */ {
/*    */ 
/*    */   @Autowired
/*    */   private DepartmentService departmentService;
/*    */ 
/*    */   @Autowired
/*    */   private UserService userService;
/*    */ 
/*    */   @RequestMapping({"/list"})
/*    */   public ModelAndView list(@RequestParam(value="page", required=false) String page, Department s_department, HttpServletRequest request)
/*    */   {
/* 43 */     ModelAndView mav = new ModelAndView();
/* 44 */     HttpSession session = request.getSession();
/* 45 */     if (StringUtil.isEmpty(page)) {
/* 46 */       page = "1";
/* 47 */       session.setAttribute("s_department", s_department);
/*    */     } else {
/* 49 */       s_department = (Department)session.getAttribute("s_department");
/*    */     }
/* 51 */     PageBean pageBean = new PageBean(Integer.parseInt(page), 3);
/* 52 */     List departmentList = this.departmentService.find(pageBean, s_department);
/* 53 */     int total = this.departmentService.count(s_department);
/* 54 */     String pageCode = PageUtil.getPagation(request.getContextPath() + "/department/list.do", total, Integer.parseInt(page), 3);
/* 55 */     mav.addObject("pageCode", pageCode);
/* 56 */     mav.addObject("modeName", "部门管理");
/* 57 */     mav.addObject("departmentList", departmentList);
/* 58 */     mav.addObject("mainPage", "jsp/department/list.jsp");
/* 59 */     mav.setViewName("main");
/* 60 */     return mav;
/*    */   }
/*    */ 
/*    */   @RequestMapping({"/preSave"})
/*    */   public ModelAndView preSave(@RequestParam(value="id", required=false) String id) {
/* 65 */     ModelAndView mav = new ModelAndView();
/* 66 */     mav.addObject("mainPage", "jsp/department/save.jsp");
/* 67 */     mav.addObject("modeName", "部门管理");
/* 68 */     mav.setViewName("main");
/* 69 */     if (StringUtil.isNotEmpty(id)) {
/* 70 */       mav.addObject("actionName", "部门修改");
/* 71 */       Department department = this.departmentService.loadById(Integer.parseInt(id));
/* 72 */       mav.addObject("department", department);
/*    */     } else {
/* 74 */       mav.addObject("actionName", "部门添加");
/*    */     }
/* 76 */     return mav;
/*    */   }
/*    */ 
/*    */   @RequestMapping({"/save"})
/*    */   public String save(Department department) {
/* 81 */     if (department.getId() == null)
/* 82 */       this.departmentService.add(department);
/*    */     else {
/* 84 */       this.departmentService.update(department);
/*    */     }
/* 86 */     return "redirect:/department/list.do";
/*    */   }
/*    */ 
/*    */   @RequestMapping({"/delete"})
/*    */   public void delete(@RequestParam("id") String id, HttpServletResponse response) throws Exception {
/* 91 */     JSONObject result = new JSONObject();
/* 92 */     if (this.userService.existUserByDeptId(Integer.parseInt(id))) {
/* 93 */       result.put("errorInfo", "该部门下存在用户，不能删除！");
/*    */     } else {
/* 95 */       this.departmentService.delete(Integer.valueOf(id).intValue());
/* 96 */       result.put("success", Boolean.valueOf(true));
/*    */     }
/* 98 */     ResponseUtil.write(result, response);
/*    */   }
/*    */ }

/* Location:           C:\Users\arcgis\Desktop\新建文件夹\a0\[www.java1234.com]《SpringMvc设备管理系统》第零讲源码及文档\EquipmentSys\WEB-INF\classes\
 * Qualified Name:     com.java1234.controller.DepartmentController
 * JD-Core Version:    0.6.1
 */